package com.rogo.openapi.flickr.core
{
	import com.rogo.oauth.URLParam;
	import com.rogo.oauth.URLParams;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.navigateToURL;
	
	/**
	 * Flickr OAuth
	 * 
	 * @author Rogo
	 */	
	public class FlickrOAuth extends FlickrAPI
	{
		public function FlickrOAuth()
		{
			super();
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		public static const URL_REQUEST_TOKEN:String = 'http://www.flickr.com/services/oauth/request_token';
		public static const URL_USER_AUTHORIZATION:String = 'http://www.flickr.com/services/oauth/authorize';
		public static const URL_ACCESS_TOKEN:String = 'http://www.flickr.com/services/oauth/access_token';
		
		
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		public var key:String;
		public var secret:String;
		public var callback:String;
		
		private var requestTokenLoader:URLLoader;
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取请求令牌
		 */		
		public function requestToken():void
		{
			// set oauth key
			oauthKey.oauth_consumer_key = key;
			oauthKey.oauth_consumer_secret = secret;
			
			// oob = out of box
			if (!callback)
				callback = 'oob';
			else
				callback = encodeURIComponent(callback);
			
			var urlParams:URLParams = new URLParams();
			urlParams.addParam(new URLParam('oauth_callback', callback));
			
			var urlReq:URLRequest = getSignatureUrlReq(URLRequestMethod.GET, 
				URL_REQUEST_TOKEN, urlParams, oauthKey);
			
			load(urlReq, requestToken_resultHandler, requestToken_errorHandler);
		}
		
		/**
		 * 用户授权
		 */		
		public function userAuthorization():void
		{
			var urlParams:URLParams = new URLParams();
			urlParams.addParam(new URLParam('oauth_token', oauthKey.oauth_token));
			
			var urlReq:URLRequest = getUrlReq(URLRequestMethod.GET, 
				URL_USER_AUTHORIZATION, urlParams);
			
			navigateToURL(urlReq);
		}
		
		/**
		 * 获取访问令牌
		 */		
		public function accessToken():void
		{
			var urlParams:URLParams = new URLParams();
			urlParams.addParam(new URLParam('oauth_verifier', oauthKey.oauth_verifier));
			urlParams.addParam(new URLParam('oauth_token', oauthKey.oauth_token));
			
			var urlReq:URLRequest = getSignatureUrlReq(URLRequestMethod.GET, 
				URL_ACCESS_TOKEN, urlParams, oauthKey);
			
			load(urlReq, accessToken_resultHandler, accessToken_errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Event Handlers
		//
		//--------------------------------------------------------------------------
		
		private function requestToken_resultHandler(e:Event):void
		{
			//oauth_callback_confirmed=true
			//&oauth_token=72157626737672178-022bbd2f4c2f3432
			//&oauth_token_secret=fccb68c4e6103197
			
			var result:String = e.currentTarget.data;
			
			oauthKey.oauth_token = 
				result.split('&')[1].toString().split('=')[1].toString();
			oauthKey.oauth_token_secret = 
				result.split('&')[2].toString().split('=')[1].toString();
			
			userAuthorization();
		}
		
		private function requestToken_errorHandler(e:Event):void
		{
			// request token error
		}
		
		private function accessToken_resultHandler(e:Event):void
		{
			// fullname=Jamal%20Fanaian
			// &oauth_token=72157626318069415-087bfc7b5816092c
			// &oauth_token_secret=a202d1f853ec69de
			// &user_nsid=21207597%40N07
			// &username=jamalfanaian
			var result:String = e.currentTarget.data;
			
			oauthKey.oauth_token = 
				result.split('&')[1].toString().split('=')[1].toString();
			oauthKey.oauth_token_secret = 
				result.split('&')[2].toString().split('=')[1].toString();
		}
		
		private function accessToken_errorHandler(e:Event):void
		{
			// access token error
		}
		
	}
}